<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\FileUpload;
use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Community;
use app\api\model\v1\ProjectSample;
use app\BaseController;
use think\Request;

class Sample extends BaseController
{

    use FileUpload,SearchDataForModel;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/30 9:52
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 样本点列表
     */
    public function getSampleList(){
        $limit = $this->request->param('limit',10);
        $res = $this->search(new ProjectSample(),[],$limit,'asc',['area','street','community','project']);
        return $this->response($res);
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/30 10:11
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存样本点
     */
    public function saveSample(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['uid'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            ProjectSample::create($data);
        } else {
            $res = ProjectSample::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增样本点' : '编辑样本点', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/30 9:50
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 导入样本点
     */
    public function importSampleExcel(){
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');
        $result = $this->saveImportExcel($file, 'import_excel',$type='sample');
        $is_bool = ProjectSample::saveSampleExcel($result,$this->request->uid);
        if ($is_bool === 200) {
            //日志
            AdminLog::record('导入样本点', $this->request->param(), $this->request->uid, $this->request->username);
            return $this->response();
        } else {
            return $this->response(403, '导入问卷失败');
        }
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/30 10:14
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除样本点
     */
    public function deleteSample($id)
    {
        $res = ProjectSample::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        //->force()
        $res->delete();
        AdminLog::record('删除样本点', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/30 14:42
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 微信端样本列表--数据源 'community_id'=>$data['community_id'],
     */
    public function getSampleWxList(){
        $data = $this->request->param();
        $type = $this->request->param('type','wx');
        if ($type === 'pc'){
            $res = ProjectSample::field('id as value,sample_title as label')->where(['community_id'=>$data['community_id'],'project_id'=>$data['project_id']])->select();
            return $this->response($res);
        }else{
            $map = [];
            if (!$this->auth->isSuperAdmin($this->request->wx_id)){

                $map[] = array('community_id','=',$data['community_id']);
            }
            $map[] = array('project_id','=',$data['project_id']);
            $res = ProjectSample::where($map)->select();
            if ($res->isEmpty()){
                $empty_arr[] = [
                   'id'=>-1,
                   'sample_title'=>'自定义样本点'
                ];
                return $this->response(array($empty_arr));
            }else{
                $new_sample = $res->toArray();
                $empty_arr = [
                    'id'=>-1,
                    'sample_title'=>'自定义样本点'
                ];
                array_unshift($new_sample, $empty_arr);

                return $this->response(array($new_sample));
            }

        }
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/5 14:48
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 主次干道
     */
    public function getSampleRoads(){
        $data = $this->request->param();
        $map = [];
//        if ($this->request->has('community_id') && !empty($this->request->param('community_id'))) {
//            $map[] = ['community_id', 'in', $this->request->param('community_id')];
//        }
        if ($this->request->has('project_id') && !empty($this->request->param('project_id'))) {
            $map[] = ['project_id', '=', $this->request->param('project_id')];
        }

        $res = ProjectSample::field('id as value,sample_title as label')->where($map)->select();
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/12 10:53
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 新增自定义样本点
     */
    public function addZdySample(){
        $data = $this->request->param();
        $data['uid'] = $this->request->wx_id;
        $res = Community::find($data['community_id']);
        if (is_null($res)){
            return $this->response(403,'无法找到该社区，请联系管理员');
        }
        $data['area_id'] = $res->area_id ?? 0;
        $data['street_id'] = $res->street_id ?? 0;
        $sample_id = ProjectSample::create($data)->id;
        $new_arr = [
            'sample_id'=>$sample_id,
            'sample_title'=>$data['sample_title']
        ];
        return $this->response($new_arr);
    }
}
